package com.anytron.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.anytron.model.AppApollo;
import com.anytron.model.req.PageRequest;
import com.anytron.model.res.ResultData;
import com.anytron.service.AppApolloService;
import com.anytron.service.AppInfoService;
import com.github.pagehelper.PageInfo;


@RestController
@RequestMapping(value = "app/apollo")
public class AppApolloController {
	
	@Autowired
	AppApolloService service;
	@Autowired
	AppInfoService appInfoService;
	
	@RequestMapping(value = "list")
	public ResultData<PageInfo<AppApollo>> list(PageRequest pageRequest,AppApollo appApollo) throws Exception {
		ResultData<PageInfo<AppApollo>> resultData = new ResultData<>();
		PageInfo<AppApollo> pageInfo = service.queryDataByPage(pageRequest, appApollo);
		resultData.setData(pageInfo);
		resultData.setErrorCode(0);
		resultData.setSuccess(true);
		return resultData;
	}
	
	@RequestMapping(value = "add" ,method = RequestMethod.POST)
	public ResultData<String> add(@RequestBody AppApollo appApollo) throws Exception {
		ResultData<String> resultData = new ResultData<>();
		AppApollo select = new AppApollo();
		select.setAppId(appApollo.getAppId());
		AppApollo one = service.getOne(select);
		if (one!=null) {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("当前应用已配置过apollo配置,请重新选择");
			resultData.setSuccess(false);
			return resultData;
		}
		int res = service.add(appApollo);
		if (res>0) {
			resultData.setErrorCode(0);
			resultData.setSuccess(true);
		}else {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("保存失败");
			resultData.setSuccess(false);
		}
		return resultData;
	}
	
	@RequestMapping(value = "update")
	public ResultData<String> update(@RequestBody AppApollo appApollo) throws Exception {
		ResultData<String> resultData = new ResultData<>();
		AppApollo select = new AppApollo();
		select.setAppId(appApollo.getAppId());
		AppApollo one = service.getOne(select);
		if (one!=null && one.getId().longValue()!=appApollo.getId().longValue()) {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("所选应用已配置过apollo配置,请重新选择");
			resultData.setSuccess(false);
			return resultData;
		}
		
		int res = service.update(appApollo);
		if (res>0) {
			resultData.setErrorCode(0);
			resultData.setSuccess(true);
		}else {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("更新失败");
			resultData.setSuccess(false);
		}
		return resultData;
	}
	
	@RequestMapping(value = "delete")
	public ResultData<String> delete(@RequestBody AppApollo appApollo) throws Exception {
		ResultData<String> resultData = new ResultData<>();
		int res = service.delete(appApollo.getId());
		if (res>0) {
			resultData.setErrorCode(0);
			resultData.setSuccess(true);
		}else {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("删除失败");
			resultData.setSuccess(false);
		}
		return resultData;
	}
}
